# INDEX VIEW
# -----------------------------------------------------------------------------
# This is the start page view.

class IndexView extends window.App.BaseView

    # Init the view.
    init: =>
        @setDom ["#txtTitle", "#txtDate", "#txtOnEndMessage", "#txtCreatorName"
                 "#chkIsPrivate", "#chkPlaySound",
                 "#moreInfo", "#addMore", "#butSubmit", ".quickDates"]
        @setEvents()

    # Bind events to DOM elements.
    setEvents: =>
        @dom.butSubmit.click @createClick

        # Each span inside the `quickDates` represent a quick date fill with a specific timing.
        # Set the click event to bind these dates to the `txtDate` element.
        @dom.quickDates.find("span").click @quickDateClick

        # CLicking on the `addMore` span will show the `moreInfo` div.
        @dom.addMore.click @showMoreInfo


    # GENERAL VIEW METHODS
    # -------------------------------------------------------------------------

    # When user clicks a quick date link, get its interval data (in minutes) and
    # add to the current date at `txtDate`.
    quickDateClick: (e) =>
        src = $ e.currentTarget
        interval = src.data "interval"
        targetDate = moment()

        # Get the first character to parse the desired datetime.
        firstChar = interval.toString().substring 0, 1
        addedValue = interval.toString().substring 1

        if firstChar is "y"
            targetDate.year targetDate.year() + parseInt(addedValue)
        else if firstChar is "m"
            targetDate.month targetDate.month() + parseInt(addedValue)
        else
            targetDate.minute targetDate.minute() + interval

        @dom.txtDate.val targetDate.format App.Settings.General.dateFormat

    # Show the `moreInfo` div. Called when user clicks the `addMore` span.
    showMoreInfo: =>
        if @dom.addMore.hasClass "down"
            @dom.moreInfo.slideDown App.Settings.UI.slideInterval, =>
                @dom.addMore.removeClass("down").addClass("up")
                @dom.addMore.find("span").html(App.Messages.lessOptions)
        else
            @dom.moreInfo.slideUp App.Settings.UI.slideInterval, =>
                @dom.addMore.removeClass("up").addClass("down")
                @dom.addMore.find("span").html(App.Messages.moreOptions)


    # CREATE COUNTDOWN
    # -------------------------------------------------------------------------

    # When user clicks the "Create" button, validate the [Countdown](countdown.html), save
    # to the server and redirect to the countdown page.
    createClick: =>
        @validateForm()

    # Validate the form before sending to the server. The validation is done
    # by the [Countdown](countdown.html) model itself, and returns the DOM field
    # which is invalid or null if it's all good.
    validateForm: =>
        cd = new App.Countdown()

        invalidField = cd.validateClientForm @dom.txtTitle, @dom.txtDate, @dom.txtOnEndMessage, @dom.txtCreatorName, @dom.chkIsPrivate, @dom.chkPlaySound

        if invalidField?
            @warnField invalidField
        else
            @submitCountdown cd

    # Submit the form with the specified [Countdown](countdown.html) model and call
    # its `save` method. If successful, redirect to the [countdown page](countdownView.html),
    # otherwise show the error message to the user.
    submitCountdown: (cd) =>
        if App.Settings.General.debug
            console.warn "IndexView.submitCountdown", cd.attributes

        cd.save {onSuccess: @saveOk, onError: @saveError}
        @dom.butSubmit.prop "disabled", true

    # Callback for successful save, redirect the user to the countdown page.
    saveOk: (cd, data) =>
        data = data[0] if data[0]?
        document.location.href = "/" + data.friendlyUrl

    # Callback for error when saving, show the error message to the user.
    saveError: (cd, error) =>
        @showAlert error


# APPEND VIEW TO WINDOW
# -----------------------------------------------------------------------------
window.App.currentView = new IndexView()